/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public abstract class Ship extends Sprite {
    public final static int DUP = 1;
    public final static int DUPRIGHT = 2;
    public final static int DRIGHT = 3;
    public final static int DDOWNRIGHT = 4;
    public final static int DDOWN = 5;
    public final static int DDOWNLEFT = 6;
    public final static int DLEFT = 7;
    public final static int DUPLEFT = 8;
    public GameManagement gameMng;
    public int direction;
    public int count;
    public Cannon a;
    public Cannon b;    
    public Rubble r;
    protected long time = System.currentTimeMillis();
    public boolean startMove;
    public boolean firing;
    protected int xDes;
    protected int yDes;
    public boolean die;
    public int sType = 0;
    public int[] frameUp = {0, 16, 32, 48, 64, 80, 96, 112};
    public int[] frameUpRight = {2, 18, 34, 50, 66, 82, 98, 114};
    public int[] frameRight = {4, 20, 36, 52, 68, 84, 100, 116};
    public int[] frameDownRight = {6, 22, 38, 54, 70, 86, 102, 118};
    public int[] frameDown = {8, 24, 40, 56, 72, 88, 104, 120};
    public int[] frameDownLeft = {10, 26, 42, 58, 74, 90, 106, 122};
    public int[] frameLeft = {12, 28, 44, 60, 76, 92, 108, 124};
    public int[] frameUpLeft = {14, 30, 46, 62, 78, 94, 110, 126};
    public int[] frameTurn = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,15};
    public int[] explode = {128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143};
    public int[] inVortex = {144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
    157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169
    , 170, 171, 172, 173, 174, 175};
    public int[][] dirc = {{     0,    24,   24,   24,     0,    -24,    -24,     -24},
                          {    -24,   -24,    0,    24,    24,    24,      0,    -24}};
    //                        1       2    3     4       5       6       7     8
    public Ship (GameManagement gameMng, Image image) throws Exception {
        
        
        super(image, 24, 24);
        startMove = false;
        count = 0;
        this.defineCollisionRectangle(12, 12, 2, 2);
        r = new Rubble(gameMng);
        die = false;
        this.gameMng = gameMng;  
        firing = false;
        a = new Cannon(this, gameMng.cannonImage);
        a.setVisible(false);
        b = new Cannon(this, gameMng.cannonImage);
        b.setVisible(false);
        direction = Math.abs(gameMng.r.nextInt() % 8) + 1;
        switch(direction) {
            
            case 1:setFrameSequence(frameUp);break;
            case 2:setFrameSequence(frameUpRight);break;
            case 3:setFrameSequence(frameRight);break;
            case 4:setFrameSequence(frameDownRight);break;
            case 5:setFrameSequence(frameDown);break;
            case 6:setFrameSequence(frameDownLeft);break;
            case 7:setFrameSequence(frameLeft);break;
            case 8:setFrameSequence(frameUpLeft);break;
        } 
    }
    
    public int moveUp (int sType) 
    {
        if(getY() > 0  && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24][getY()/24 - 1] < 2  || sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24][getY()/24 - 1] == 0)
                    gameMng.element[getX()/24][getY()/24 - 1] = sType;
                    startMove = true;
                    direction  = DUP;
                    xDes = getX() ;
                    yDes = getY() - 24;
                    return 0;//start move
                }
        }         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;//endmove
        }
        if(startMove)       
        {            
            move(0, -2);
            setFrameSequence(frameUp);
        }
        
        return 2;//moving
    }
    
    public int moveUpRight (int sType) {
        if( getY() > 0 && getX() < 216  && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24 + 1][getY()/24 - 1] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 + 1][getY()/24 - 1] == 0)
                    gameMng.element[getX()/24 + 1][getY()/24 - 1] = sType;
                    startMove = true;
                    direction  = DUPRIGHT;
                    xDes = getX() + 24;
                    yDes = getY() - 24;
                    return 0;
                }
        }         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move( 2, -2);
            setFrameSequence(frameUpRight);
        }
        return 2;
    }
    
    public int moveRight (int sType) {
        
        if( getX() < 216  && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24 + 1][getY()/24] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 + 1][getY()/24] == 0)
                    gameMng.element[getX()/24 + 1][getY()/24] = sType;
                    startMove = true;
                    direction  = DRIGHT;
                    xDes = getX() + 24;
                    yDes = getY();
                    return 0;
                }
        }
         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move(2, 0);
            setFrameSequence(frameRight);
        }
        return 2;
    } 
    
    public int moveDownRight (int sType) {
        
        if( getY() < 216 && getX() < 216 && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24 + 1][getY()/24 + 1] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 + 1][getY()/24 + 1] == 0)
                    gameMng.element[getX()/24 + 1][getY()/24 + 1] = sType;
                    startMove = true;
                    direction  = DDOWNRIGHT;
                    xDes = getX() + 24;
                    yDes = getY() + 24;
                    return 0;
                }
        }
         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move(2, 2);
            setFrameSequence(frameDownRight);
        }
         return 2;
    }
    
    public int moveDown (int sType) {
        
        if( getY() < 216  && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24][getY()/24 + 1] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24][getY()/24 + 1] == 0)
                    gameMng.element[getX()/24][getY()/24 + 1] = sType;
                    startMove = true;
                    direction  = DDOWN;
                    xDes = getX() ;
                    yDes = getY() + 24;
                     return 0;
                }
        }
         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move(0, 2);
            setFrameSequence(frameDown);
        }
         return 2;
    }
    
    public int moveDownLeft (int sType) {
        
        if(getX() > 0 && getY() < 216 && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24 - 1][getY()/24 + 1] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 - 1][getY()/24 + 1] == 0)
                    gameMng.element[getX()/24 - 1][getY()/24 + 1] = sType;
                    startMove = true;
                    direction  = DDOWNLEFT;
                    xDes = getX() - 24;
                    yDes = getY() + 24;
                     return 0;
                }
        }
         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move(-2, 2);
            setFrameSequence(frameDownLeft);
        }
         return 2;
    } 
    
    public int moveLeft (int sType) {        
        if( getX() > 0  && count == 0 && !startMove)
        {
                if(gameMng.element[getX()/24 - 1][getY()/24 ] < 2|| sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 - 1][getY()/24] == 0)
                    gameMng.element[getX()/24 - 1][getY()/24 ]  = sType;
                    startMove = true;
                    direction  = DLEFT;
                    xDes = getX() - 24;
                    yDes = getY() ;
                     return 0;
                }
        }         
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move( -2, 0);
            setFrameSequence(frameLeft);
        }
         return 2;
    }
    
    public int moveUpLeft (int sType) {        
        if(getY() > 0 && getX() > 0 && count == 0 && !startMove)
                   if(gameMng.element[getX()/24 - 1][getY()/24 - 1] < 2 || sType != 3)
                {
                    count ++;
                    gameMng.element[getX()/24][getY()/24] = 0;
                    if(gameMng.element[getX()/24 - 1][getY()/24 - 1] == 0)
                    gameMng.element[getX()/24 - 1][getY()/24 - 1] = sType;
                    startMove = true;
                    direction  = DUPLEFT;
                    xDes = getX() - 24;
                    yDes = getY() - 24;
                    
                     return 0;
                }      
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0 ;
            if(this.sType == 4)
                {
                    int d = 0;
                    if( direction == 4 ||  direction == 8) d = 2;
                    else if( direction == 1 ||  direction == 5) d = 3;
                    else if( direction == 7 ||  direction == 3) d = 1;
                    else if( direction == 2 || direction == 6) d = 4;
                    firing = true;
                    gameMng.eneFiring = true;
                    if(a.setD(d) + b.setD(d+4) == 0) 
                        firing = false;
                    else gameMng.control.shoot();
                }
            return 1;
        }
        if(startMove)       
        {            
            move( -2, -2);
            setFrameSequence(frameUpLeft);
        }
         return 2;
    }  
    abstract int autoMove(Ship s);
    public int bAutoMove(Ship s, int sType)
    {
        int res = 0;
        int oldx = s.getX();
        int oldy = s.getY();
        if(getX() > oldx)
        {
            if(getY() > oldy)
                res = moveUpLeft(sType);
            else if(getY() < oldy)
                    res = moveDownLeft(sType);
            else res = moveLeft(sType);
        }
        else
            if(getX() < oldx)
            {
                if(getY() > oldy)
                    res = moveUpRight(sType);
                else if(getY() < oldy)
                        res = moveDownRight(sType);
                else res = moveRight(sType);
            }
        else    
            if(getY() > oldy)
                res = moveUp(sType);
            else if(getY() < oldy)
                    res = moveDown(sType);
        return res;
    }
    public abstract int fire () ;
    
    public void wobble(long currTime) {
        if(currTime - time > 150 )
        {
            
            if(this.getFrameSequenceLength() == 16 && this.sType == 0)
                {
                    gameMng.burning = true;
                    if(this.getFrame() == 14)
                    {
                        gameMng.burning = false;
                        setVisible(false);
                        gameMng.layerManager.remove(this);
                    }
                    else
                        this.nextFrame();
                }
                else
                {
                    this.nextFrame();
                }
            nextFrame();   
            time = currTime;
        }
    }
    public int trans(long currTime, int newdir)
    {
        if(newdir == direction) return 1;
        
        if(getFrame() == (newdir - 1)* 2 )
        { 
            direction = newdir;
            return 1;
        }
        if(currTime - time > 100)
        {
            nextFrame();    
            time = currTime;
        }
        
        return 0;
    }
}
